-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang] Emitting a warning if optimizations are enabled with sanitizers #95934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang] Emitting a warning if optimizations are enabled with sanitizers #95934
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-clang-driver @llvm/pr-subscribers-clang Author: None (Ritanya-B-Bharadwaj) ChangesSanitizers achieve better accuracy with lower optimization levels, and it is generally recommended to use To ensure transparency, we should issue a warning when optimization levels other than Full diff: https://github.com/llvm/llvm-project/pull/95934.diff 3 Files Affected:
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 1ca2cb85565a1..bd254bcaf5d97 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -477,6 +477,8 @@ def warn_drv_disabling_vptr_no_rtti_default : Warning<
def warn_drv_object_size_disabled_O0 : Warning<
"the object size sanitizer has no effect at -O0, but is explicitly enabled: %0">,
InGroup<InvalidCommandLineArgument>, DefaultWarnNoWerror;
+def warn_sanitizer_with_optimization : Warning<
+ "enabling optimizations with sanitizers may potentially reduce effectiveness">;
def warn_ignoring_verify_debuginfo_preserve_export : Warning<
"ignoring -fverify-debuginfo-preserve-export=%0 because "
"-fverify-debuginfo-preserve wasn't enabled">,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 331cf6e713d89..2d72b3eb62308 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -6208,6 +6208,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
PScpu::addProfileRTArgs(TC, Args, CmdArgs);
PScpu::addSanitizerArgs(TC, Args, CmdArgs);
}
+
+ // Emit a warning if optimizations are enabled with sanitizers
+ if (Args.hasArg(options::OPT_fsanitize_EQ) &&
+ (Args.hasArg(options::OPT_Ofast) || Args.hasArg(options::OPT_O))) {
+ D.Diag(diag::warn_sanitizer_with_optimization);
+ }
// Pass options for controlling the default header search paths.
if (Args.hasArg(options::OPT_nostdinc)) {
diff --git a/clang/test/Driver/fsanitize.c b/clang/test/Driver/fsanitize.c
index 08e9c78f9d1d2..5657bb9a6da26 100644
--- a/clang/test/Driver/fsanitize.c
+++ b/clang/test/Driver/fsanitize.c
@@ -1038,3 +1038,10 @@
// RUN: not %clang --target=aarch64-none-elf -fsanitize=dataflow %s -### 2>&1 | FileCheck %s -check-prefix=UNSUPPORTED-BAREMETAL
// RUN: not %clang --target=arm-arm-none-eabi -fsanitize=shadow-call-stack %s -### 2>&1 | FileCheck %s -check-prefix=UNSUPPORTED-BAREMETAL
// UNSUPPORTED-BAREMETAL: unsupported option '-fsanitize={{.*}}' for target
+
+// RUN: %clang -O0 -O1 -fsanitize=address %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
+// RUN: %clang -Ofast -fsanitize=address %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
+// RUN: %clang -O3 -fsanitize=address %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
+// RUN: %clang -O2 -fsanitize=thread %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
+// RUN: %clang -O1 -fsanitize=thread %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN
+// CHECK-SAN-OPT-WARN: warning: enabling optimizations with sanitizers may potentially reduce effectiveness
|
| "the object size sanitizer has no effect at -O0, but is explicitly enabled: %0">, | ||
| InGroup<InvalidCommandLineArgument>, DefaultWarnNoWerror; | ||
| def warn_sanitizer_with_optimization : Warning< | ||
| "enabling optimizations with sanitizers may potentially reduce effectiveness">; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| "enabling optimizations with sanitizers may potentially reduce effectiveness">; | |
| "enabling optimizations may reduce the effectiveness of sanitizers">; |
| // RUN: %clang -O3 -fsanitize=address %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN | ||
| // RUN: %clang -O2 -fsanitize=thread %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN | ||
| // RUN: %clang -O1 -fsanitize=thread %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-SAN-OPT-WARN | ||
| // CHECK-SAN-OPT-WARN: warning: enabling optimizations with sanitizers may potentially reduce effectiveness |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // CHECK-SAN-OPT-WARN: warning: enabling optimizations with sanitizers may potentially reduce effectiveness | |
| // CHECK-SAN-OPT-WARN: warning: enabling optimizations may reduce the effectiveness of sanitizers |
|
ping |
AaronBallman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not certain we should issue a diagnostic when optimizations are enabled with sanitizers. For example, there is explicit documentation that users should use -O1 with address sanitizer and similarly, users should use -O2 with the thread sanitizer.
So perhaps this is a matter that's better handled with documentation so we can explain the tradeoffs to users?
|
I agree that we should not add a driver diagnostic. |
MaskRay
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.
Sanitizers achieve better accuracy with lower optimization levels, and it is generally recommended to use
-O0(the default optimization level) when using sanitizers for the most accurate results. However, many users might not be aware of this recommendation.To ensure transparency, we should issue a warning when optimization levels other than
-O0are enabled alongside sanitizers. This warning will inform users that higher optimization levels can reduce the effectiveness of sanitizers, thereby making them aware of the potential impact on the accuracy of sanitizer.